package com.sky.controller.admin;


import com.sky.dto.AddDishDto;
import com.sky.dto.DishPageQueryDTO;
import com.sky.dto.UpdateDishDto;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 菜品 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-06-25
 */
@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品")
public class DishController {
    @Autowired
    private DishService dishService;

    /**
     * 根据id查菜品
     *
     * @param id
     * @return
     */
    @GetMapping("{id}")
    @ApiOperation("根据id查菜品")
    public Result getDishById(@PathVariable Long id) {
        Dish dish = dishService.getById(id);
        return Result.success(dish);
    }

    /**
     * 根据分类查菜品
     *
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类查菜品")
    public Result getDishByCategoryId(Long categoryId) {
        List<Dish> dish = dishService.getDishByCategoryId(categoryId);
        return Result.success(dish);
    }

    /**
     * 新增菜品
     *
     * @param dto
     * @return
     */
    @PostMapping()
    @ApiOperation("新增菜品")
    public Result addDish(@RequestBody AddDishDto dto) {
        dishService.addDish(dto);
        return Result.success();
    }

    /**
     * 修改菜品状态
     *
     * @param status
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("修改菜品状态")
    public Result updateDishStatus(@PathVariable Integer status, @RequestBody Long id) {
        dishService.updateDishStatus(status, id);
        return Result.success();
    }

    /**
     * 修改菜品
     *
     * @param dto
     * @return
     */
    @PutMapping()
    @ApiOperation("修改菜品")
    public Result updateDish(@RequestBody UpdateDishDto dto) {
        dishService.updateDish(dto);
        return Result.success();
    }

    /**
     * 删除菜品
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("删除菜品")
    public Result delete(@RequestParam("ids") List<Long> ids) {
        dishService.deleteByIds(ids);
        return Result.success();
    }

    /**
     * 菜品分页查询
     * @param dto
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result page(DishPageQueryDTO dto) {
        PageResult pageResult = dishService.pageQuery(dto);
        return Result.success(pageResult);
    }
}
